<html lang="en">
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css"/>
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css"/>
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/js/toastr/toastr.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/toastr/toastr.css">
</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->

<div class="box-header with-border">
    <h3 class="box-title">商品类型模板管理</h3>
</div>

<div class="box-body">

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal"
                            data-target="#add"><i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" title="删除" onclick="deleteInfo()"><i class="fa fa-trash-o"></i> 删除</button>

                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i
                            class="fa fa-refresh"></i> 刷新
                    </button>
                </div>
            </div>
        </div>
        <div class="box-tools pull-right">
            <div class="has-feedback">
                分类模板名称：<input name="name" id="name">
                <button class="btn btn-default" onclick="show()">查询</button>
            </div>
        </div>
        <!--工具栏/-->

        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">模板ID</th>
                <th class="sorting">分类模板名称</th>
                <th class="sorting">关联品牌</th>
                <th class="sorting">关联规格</th>
                <th class="sorting">扩展属性</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="template">
            <script>
                $(function () {
                    show();
                })

                function show() {
                    $.ajax({
                        url: "/template/getTemplate",
                        data: {
                            name: $("#name").val(),
                            pageNum: $("#pageNumber").val(),
                            pageSize: $("#pageSize").val()
                        },
                        type: "get", //请求方式 get post 默认get
                        dataType: "json", //返回值类型 html json text xml
                        async: true,//true代表异步请求 false代表同步请求
                        success: function (result) {//成功回调函数
                            var page = result.data.rows;
                            var div = "";
                            for (var i = 0; i < page.length; i++) {
                                div += "<tr>";
                                div += '<td><input type="checkbox" name="check" value="' + page[i].id + '"></td>';
                                div += '<td>' + page[i].id + '</td>';
                                div += '<td>' + page[i].name + '</td>';
                                var specIdsList = JSON.parse(page[i].specIds);
                                var brandIdsList = JSON.parse(page[i].brandIds);
                                var customdsList = JSON.parse(page[i].customAttributeItems);
                                var arr = "";
                                var arr1 = "";
                                var arr2 = "";
                                for (var a = 0; a < brandIdsList.length; a++) {
                                    arr1 += brandIdsList[a].text + ",";
                                }
                                if (arr1.length > 0) {
                                    arr1 = arr1.substr(0, arr1.length - 1);
                                }
                                div += '<td>' + arr1 + '</td>';
                                for (var z = 0; z < specIdsList.length; z++) {
                                    arr += specIdsList[z].text + ",";
                                }
                                if (arr.length > 0) {
                                    arr = arr.substr(0, arr.length - 1);
                                }
                                div += '<td>' + arr + '</td>';
                                for (var s = 0; s < customdsList.length; s++) {
                                    arr2 += customdsList[s].text + ",";
                                }
                                if (arr2.length > 0) {
                                    arr2 = arr2.substr(0, arr2.length - 1);
                                }
                                div += '<td>' + arr2 + '</td>';
                                div += '<td className="text-center">';
                                div += '<button type="button" className="btn bg-olive btn-xs" data-toggle="modal"data-target="#update" onclick="toUpdate(' + page[i].id + ')">修改</button> </td></tr>';
                            }
                            $("#template").html(div);
                            install_page(result.data);
                        },
                        error: function (i) {//发生错误回调函数
                            alert("异常错误")
                        }
                    })
                }

                function deleteInfo() {
                    var rows = $("[name=check]:checked");
                    var ids = [];
                    $(rows).each(function (i, e) {
                        ids[i] = e.value;
                    })
                    if (ids.length == 0) {
                        toastr.success("请选择要删除的数据");
                        return;
                    }

                        $.ajax({
                            url: "/template/deleteInfo/" + ids,
                            type: "get", //请求方式 get post 默认get
                            dataType: "json", //返回值类型 html json text xml
                            async: true,//true代表异步请求 false代表同步请求
                            success: function (result) {//成功回调函数
                                if (result.code == 10000) {
                                    toastr.success("删除成功");
                                    setTimeout("refresh()", 1000);//3秒后执行refresh()
                                } else {
                                    toastr.error("删除失败");
                                    setTimeout("refresh()", 1000);//3秒后执行refresh()
                                }

                            },
                            error: function (i) {//发生错误回调函数
                                alert("异常错误")
                            }
                        })
                }
            </script>
            </tbody>

        </table>
        <div th:insert="common/page.html"></div>
        <!--数据列表/-->


    </div>
    <!-- 数据表格 /-->


</div>
<!-- /.box-body -->


<!-- 增加窗口 -->
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">商品类型模板编辑</h3>
            </div>
            <div class="modal-body">

                <table class="table table-bordered table-striped" width="800px">
                    <tr>
                        <td>商品类型</td>
                        <td><input class="form-control" id="name1" placeholder="商品类型"></td>
                    </tr>
                    <tr>
                        <td>关联品牌</td>
                        <td>
                            <!-- 下拉框多选项-->
                            <select id="brand" style="width: 100%" multiple>
                                <option value="-1" selected hidden disabled>--请选择--</option>
                                <option th:each="brand : ${brandList}" th:value="${brand.id}">[[${brand.name}]]</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>关联规格</td>
                        <td>
                            <!-- 下拉框多选项-->
                            <select id="spec" style="width: 100%" multiple>
                                <option value="-1" selected hidden disabled>--请选择--</option>
                                <option th:each="spec : ${specList}" th:value="${spec.id}">[[${spec.specName}]]</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>扩展属性</td>
                        <td>
                            <div class="btn-group">
                                <button type="button" class="btn btn-default" title="新增扩展属性" onclick="add1()"><i
                                        class="fa fa-file-o"></i> 新增扩展属性
                                </button>

                            </div>
                            <table class="table table-bordered table-striped" width="800px">
                                <thead>
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td>属性名称</td>
                                    <td>操作</td>
                                </tr>
                                </thead>
                                <tbody id="custom">
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td><input class="form-control" placeholder="属性名称"></td>
                                    <td>
                                        <button type="button" class="btn btn-default" title="删除"
                                                onclick="deleteOneTr(this)"><i
                                                class="fa fa-trash-o"></i> 删除
                                        </button>
                                    </td>
                                </tr>
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td><input class="form-control" placeholder="属性名称"></td>
                                    <td>
                                        <button type="button" class="btn btn-default" title="删除"
                                                onclick="deleteOneTr(this)"><i
                                                class="fa fa-trash-o"></i> 删除
                                        </button>
                                    </td>
                                </tr>
                                </tbody>
                            </table>

                        </td>
                    </tr>

                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>
<script>
    function toUpdate(id) {
        $.ajax({
            url: "/template/toUpdate",
            data: {id: id},
            type: "get", //请求方式 get post 默认get
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                var t = result.data;
                $("#id1").val(id);
                $("#name2").val(t.name);
                var brandStr = t.brandIds;
                var specStr = t.specIds;
                var customStr = t.customAttributeItems;
                var brandJson = JSON.parse(brandStr);
                var specJson = JSON.parse(specStr);
                var customJson = JSON.parse(customStr);
                var count = customJson.length - 2;
                if( count > 0){
                    for (var i = 0; i < count; i++) {
                        add2();//回显判断数据和表格是否一致,不一致那就进行加表格
                    }
                }
                var ids = [];
                var ids1 = [];
                var arr = "";
                for (var i = 0; i < brandJson.length; i++) {
                    ids.push(brandJson[i].id);
                }
                for (var i = 0; i < specJson.length; i++) {
                    ids1.push(specJson[i].id);
                }
                $("#brand1").val(ids).trigger("change");
                $("#spec1").val(ids1).trigger("change");

                var trList = $("#customHX").find("tr");
                for (var i = 1; i < trList.length; i++) {//i=0时 tr的行时标题行,可以直接跳过去
                    var tdList = $(trList[i]).find("td");
                    var arr = "";
                    arr += customJson[i - 1].text + ",";
                    if (arr.length > 0) {
                        arr = arr.substr(0, arr.length - 1);
                    }
                    $(tdList[1]).find("input").val(arr);
                }

            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }
        })
    }

    function updateInfo() {
        var arr = [];
        var arr1 = [];
        var brandList = $("#brand1").select2('data');
        var specList = $("#spec1").select2('data');
        for (var i = 0; i < brandList.length; i++) {
            var id = brandList[i].id;
            var text = brandList[i].text;
            var brandJson = {id: id, text: text};
            arr.push(brandJson);
        }
        for (var i = 0; i < specList.length; i++) {
            var id = specList[i].id;
            var text = specList[i].text;
            var specJson = {id: id, text: text};
            arr1.push(specJson);
        }
        var brandStr = JSON.stringify(arr);
        var specStr = JSON.stringify(arr1);

        //扩展属性
        var arr2 = [];
        var trList = $("#customHX").find("tr");
        for (var i = 1; i < trList.length; i++) {//i=0时 tr的行时标题行,可以直接跳过去
            var tdList = $(trList[i]).find("td");
            var custom = $(tdList[1]).find("input").val();
            var customJson = {text: custom};
            arr2.push(customJson);
        }
        //转换为字符串
        var customStr = JSON.stringify(arr2);

        $.ajax({
            url: "/template/updateInfo",
            data: {id:$("#id1").val(),name: $("#name2").val(), brandIds: brandStr, specIds: specStr, customAttributeItems: customStr},
            type: "get", //请求方式 get post 默认get
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                if (result.code == 10000) {
                    toastr.success("修改成功")
                    setTimeout("refresh()", 1000);//3秒后执行refresh()
                } else {
                    toastr.error("修改失败")
                    setTimeout("refresh()", 1000);//3秒后执行refresh()
                }
            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }
        })
    }

    //刷新函数
    function refresh() {
        window.location.reload();
    }
</script>
<!-- 修改窗口 -->
<div class="modal fade" id="update" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" onclick="refresh()">×</button>
                <h3 id="myModalLabel1">商品类型模板编辑</h3>
            </div>
            <div class="modal-body">

                <table class="table table-bordered table-striped" width="800px">
                    <!--修改的id--> <input type="hidden" id="id1" name="id">
                    <tr>
                        <td>商品类型</td>
                        <td><input class="form-control" id="name2" placeholder="商品类型"></td>
                    </tr>
                    <tr>
                        <td>关联品牌</td>
                        <td>
                            <!-- 下拉框多选项-->
                            <select id="brand1" style="width: 100%" multiple>
                                <option value="-1" selected hidden disabled>--请选择--</option>
                                <option th:each="brand : ${brandList}" th:value="${brand.id}">[[${brand.name}]]</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>关联规格</td>
                        <td>
                            <!-- 下拉框多选项-->
                            <select id="spec1" style="width: 100%" multiple>
                                <option value="-1" selected hidden disabled>--请选择--</option>
                                <option th:each="spec : ${specList}" th:value="${spec.id}">[[${spec.specName}]]</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>扩展属性</td>
                        <td>
                            <div class="btn-group">
                                <button type="button" class="btn btn-default" title="新增扩展属性" onclick="add2()"><i
                                        class="fa fa-file-o"></i> 新增扩展属性
                                </button>

                            </div>
                            <table class="table table-bordered table-striped" width="800px" id="customHX">
                                <thead>
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td>属性名称</td>
                                    <td>操作</td>
                                </tr>
                                </thead>
                                <tbody id="custom1">
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td><input class="form-control" placeholder="属性名称"></td>
                                    <td>
                                        <button type="button" class="btn btn-default" title="删除"
                                                onclick="deleteOneTr1(this)"><i
                                                class="fa fa-trash-o"></i> 删除
                                        </button>
                                    </td>
                                </tr>
                                <tr>
                                    <td><input type="checkbox" class="icheckbox_square-blue"></td>
                                    <td><input class="form-control" placeholder="属性名称"></td>
                                    <td>
                                        <button type="button" class="btn btn-default" title="删除"
                                                onclick="deleteOneTr1(this)"><i
                                                class="fa fa-trash-o"></i> 删除
                                        </button>
                                    </td>
                                </tr>
                                </tbody>
                            </table>

                        </td>
                    </tr>

                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateInfo()">保存
                </button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true" onclick="refresh()">关闭</button>
            </div>
        </div>
    </div>
</div>
</body>

</html>
<script>
    $(function () {
        $("#brand").select2({})
        $("#spec").select2({})
        $("#brand1").select2({})
        $("#spec1").select2({})
    })

    function add() {
        var arr = [];
        var arr1 = [];
        var brandList = $("#brand").select2('data');
        var specList = $("#spec").select2('data');
        for (var i = 0; i < brandList.length; i++) {
            var id = brandList[i].id;
            var text = brandList[i].text;
            var brandJson = {id: id, text: text};
            arr.push(brandJson);
        }
        for (var i = 0; i < specList.length; i++) {
            var id = specList[i].id;
            var text = specList[i].text;
            var specJson = {id: id, text: text};
            arr1.push(specJson);
        }
        var brandStr = JSON.stringify(arr);
        var specStr = JSON.stringify(arr1);

        //扩展属性
        var arr2 = [];
        var trList = $("#custom").find("tr");
        for (var i = 0; i < trList.length; i++) {//i=0时 tr的行时标题行,可以直接跳过去
            var tdList = $(trList[i]).find("td");
            var custom = $(tdList[1]).find("input").val();
            var customJson = {text: custom};
            arr2.push(customJson);
        }
        //转换为字符串
        var customStr = JSON.stringify(arr2);

        $.ajax({
            url: "/template/addTemplate",
            data: {name: $("#name1").val(), brandIds: brandStr, specIds: specStr, customAttributeItems: customStr},
            type: "get", //请求方式 get post 默认get
            dataType: "json", //返回值类型 html json text xml
            async: true,//true代表异步请求 false代表同步请求
            success: function (result) {//成功回调函数
                if (result.code == 10000) {
                    toastr.success("增加成功")
                    setTimeout("refresh()", 2000);//2秒后执行refresh()
                } else {
                    toastr.error("增加失败")
                    setTimeout("refresh()", 2000);//2秒后执行refresh()
                }
            },
            error: function (i) {//发生错误回调函数
                alert("异常错误")
            }
        })
    }

    //刷新函数
    function refresh() {
        window.location.reload();
    }

</script>
<script type="text/javascript">
    //增加
    function add1() {   //增加一行
        var a = document.getElementById("custom"); //定位在tbody 拿出值
        var tr = document.createElement("tr");   //创建一个行<tr>
        tr.innerHTML = '<td><input type="checkbox"  className="icheckbox_square-blue"></td><td><input className="form-control"  style="width: 100%" placeholder="属性名称"></td><td><button type="button" className="btn btn-default" title="删除" onClick="deleteOneTr(this)"><i className="fa fa-trash-o"></i> 删除</button></td>';
        //上面可能会有一点点乱希望大家仔细看看
        a.appendChild(tr);	//将创建的<tr>加入到定位的tbody下
    }

    function deleteOneTr(obj) { //删除当前行(这里我就不写 script 这个格式了)
        var a = document.getElementById("custom"); //一样 需要找到 表格的父类
        var a1 = obj.parentNode.parentNode;  // 找到需要删除的位置 ，当按钮触发时直接传入自己进来 这样就拿到了位置
        a.removeChild(a1);  // 直接 remove 这个子类
    }

    //修改
    function add2() {   //增加一行
        var a = document.getElementById("custom1"); //定位在tbody 拿出值
        var tr = document.createElement("tr");   //创建一个行<tr>
        tr.innerHTML = '<td><input type="checkbox"  className="icheckbox_square-blue"></td><td><input className="form-control"  style="width: 100%" placeholder="属性名称"></td><td><button type="button" className="btn btn-default" title="删除" onClick="deleteOneTr1(this)"><i className="fa fa-trash-o"></i> 删除</button></td>';
        //上面可能会有一点点乱希望大家仔细看看
        a.appendChild(tr);	//将创建的<tr>加入到定位的tbody下
    }

    function deleteOneTr1(obj) { //删除当前行(这里我就不写 script 这个格式了)
        var a = document.getElementById("custom1"); //一样 需要找到 表格的父类
        var a1 = obj.parentNode.parentNode;  // 找到需要删除的位置 ，当按钮触发时直接传入自己进来 这样就拿到了位置
        a.removeChild(a1);  // 直接 remove 这个子类
    }

</script>
